import {
  Body,
  Controller,
  Get,
  HttpException,
  Post,
  UseGuards,
} from '@nestjs/common';
import { RoleService } from '../services/role.service';
import { ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger';
import { AuthorBaseControll } from './authorBase.controller';
import { AuthGuard } from '../guards/authority.guard';
import { addRoleDto, delRoleDto } from '../dtos/role.dto';

@ApiTags('角色管理')
@Controller('/role')
@UseGuards(AuthGuard)
export class RoleController extends AuthorBaseControll {
  constructor(private readonly roleService: RoleService) {
    super();
  }

  @Get('/getRoleList')
  @ApiOperation({ summary: '获取角色列表' })
  async getRoleList() {
    return await this.roleService.getRoleList();
  }

  @Post('/addRole')
  @ApiOperation({ summary: '新增角色' })
  @ApiConsumes('application/x-www-form-urlencoded')
  async addRole(@Body() addRoleDto: addRoleDto) {
    const result = await this.roleService.addRole(addRoleDto);
    if (result.affectedRows === 1) {
      return {
        message: '添加成功',
      };
    } else {
      throw new HttpException('添加失败', 401);
    }
  }

  @Post('/delRole')
  @ApiOperation({ summary: '删除角色' })
  async delRole(@Body() delRoleDto: delRoleDto) {
    console.log(delRoleDto);
    const result = await this.roleService.delRole(delRoleDto);
    if (result.affectedRows === 1) {
      return {
        message: '删除成功',
      };
    } else {
      throw new HttpException('删除失败', 401);
    }
  }
}
